const KoaRouter = require("@koa/router");
const userController = require("./../controller/user.controller");
const { verifyRegisterParams, handlePassword, verifyUpdateParams } = require("../middleware/user.middleware");
const { verifyAuthorization } = require("../middleware/login.middleware");
const verifyResourceExists = require("../middleware/exists.middleware");

// 1.创建路由对象
const userRouter = new KoaRouter({ prefix: "/users" });

// 2.定义路由映射

/**
 * @description 用户注册接口
 */
// 代码会越来越多 不好维护 抽离到controller中
userRouter.post("/register", verifyRegisterParams, handlePassword, userController.register);

userRouter.get("/avatar/:userId", verifyResourceExists, userController.getAvatarByUserId);

// 更新用户信息
userRouter.patch(
    "/update/:userId",
    verifyAuthorization,
    verifyResourceExists,
    verifyUpdateParams,
    handlePassword,
    userController.updateUserNameOrPassword
);

// 注销账户(销户)
userRouter.delete("/:userId", verifyAuthorization, verifyResourceExists, userController.accountCancellationByUserId);

// 3.导出路由对象
module.exports = userRouter;
